import java.util.Random;
import java.util.Scanner;

public class NGuessNumber {
    /**
     * 生成四个0-9的随机数
     */
    public int[] generateAnswer(){
        Random random = new Random();
        //包括四个数的数组
        int [] shu = new int[4];
        //判断是否相等
        boolean b =true;
        for (int i = 0; i < 4;) {
            int suiji= random.nextInt(10);
            for (int j = 0; j <4 ; j++) {
                if (suiji ==shu[j]){
                    b=false;
                    break;
                }
            }
            if (b){
                shu[i]=suiji;
                i++;
            }
            b=true;
        }
        return shu;
    }
    /**
     * 接收玩家输入方法
     */
    public int[] getPlayerInput(){
        //接受用户输入数组
        int [] shuru = new int[4];
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 4; i++) {
//            将用户输入传进数组
            shuru[i]=scanner.nextInt();
        }
        return shuru;
    }

    /**
     * 比较之后返回提示信息
     */
    public int compareGuessAnswer(int []shu,int []shuru){
        int a=0,b=0;
        for (int i = 0; i < shu.length; i++) {
            for (int j = 0; j < shuru.length; j++) {
                if (shu[i]==shuru[j]){
                    if (i==j) a++;
                    else b++;
                }
            }
        }
        if (a!=4){
            System.out.println(a+" A"+" "+b+" B");
        }
        return a;
    }

    /**
     * 判断游戏是否获胜
     */
    public boolean iswin (int a){
        if (a == 4){
            System.out.println("恭喜您答对了");
            return false;
        }
        return true;
    }

    /**
     * 判断玩家是否继续
     */
    public boolean jixu() {
        System.out.println("您是否继续游戏 Y/N");
        Scanner scanner = new Scanner(System.in);
        boolean panduan = true;
        while (true) {
            String shuru = scanner.next();
            if (shuru.equals("Y") || shuru.equals("y")) {
                panduan = true;
                break;
            } else if (shuru.equals("N") || shuru.equals("n")) {
                panduan = false;
                break;
            } else {
                System.out.println("输入错误重新输入");
            }
        }
        return panduan;
    }
    public static void main(String[] args) {

        NGuessNumber n = new NGuessNumber();
        boolean b = true;
        boolean jixu =true;
        while (jixu){
            int shu[] =n.generateAnswer();
            System.out.println("游戏开始，输入四位 1-9的数字");
        while (b){
        int shuru[] =n.getPlayerInput();
        int a= n.compareGuessAnswer(shu,shuru);
         b= n.iswin(a);
        }
        b=true;
        jixu = n.jixu();
    }
    }
}
